package com.cssw.fyzb.busi.api.system;

import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.vo.system.ParseWordVo;
import com.cssw.fyzb.busi.service.system.WordCmdService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author YangYiQun
 */
@Function(description = "解析口令", parameters = {
        @Parameter(name = "content", type = Type.Integer, description = "口令内容", required = true),
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = false),
})
@Component
public class ParseWordCmd implements Action {

    private static final Logger logger = LoggerFactory.getLogger(ParseWordCmd.class);

    @Autowired
    private WordCmdService wordCmdService;

    @Override
    public Response execute(Request request) throws Exception {
        String content = request.getString("content");
        Long userId = request.getLong("userId");
        try {
            ParseWordVo vo = wordCmdService.parseWordCmd(userId, content);
            return Response.success().put("data", vo);
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("操作异常");
        }
    }

}
